﻿
var url = window.location.href;
if(url.indexOf("www.duoec.com")==-1){
	//window.location.href = "http:/"+"/www.duoec.com"; 
};
/****/
var hi = null;
/*
* decrypt the encrypted string to the original string 
*
* return  the original string  
*/
function d(data,firstKey,secondKey,thirdKey){
 var leng = data.length;
 var decStr = "";
 var firstKeyBt,secondKeyBt,thirdKeyBt,firstLength,secondLength,thirdLength;
 if(firstKey != null && firstKey != ""){    
   firstKeyBt = kb(firstKey);
   firstLength = firstKeyBt.length;
 }
 if(secondKey != null && secondKey != ""){
   secondKeyBt = kb(secondKey);
   secondLength = secondKeyBt.length;
 }
 if(thirdKey != null && thirdKey != ""){
   thirdKeyBt = kb(thirdKey); 
   thirdLength = thirdKeyBt.length;
 }
 
 var iterator = parseInt(leng/16);
 if(hi[1]!='duoec'){iterator=0;} //===================================验证
 var i=0;  
 for(i = 0;i < iterator;i++){
   var tempData = data.substring(i*16+0,i*16+16);    
   var strByte = htb(tempData);    
   var intByte = new Array(64);
   var j = 0;
   for(j = 0;j < 64; j++){
     intByte[j] = parseInt(strByte.substring(j,j+1));
   }    
   var decByte;
   if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
     var tempBt;
     var x,y,z;
     tempBt = intByte;
     for(x = thirdLength - 1;x >= 0;x --){
       tempBt = dec(tempBt,thirdKeyBt[x]);
     }
     for(y = secondLength - 1;y >= 0;y --){
       tempBt = dec(tempBt,secondKeyBt[y]);
     }
     for(z = firstLength - 1;z >= 0 ;z --){
       tempBt = dec(tempBt,firstKeyBt[z]);
     }
     decByte = tempBt;
   }else{
     if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
       var tempBt;
       var x,y,z;
       tempBt = intByte;
       for(x = secondLength - 1;x >= 0 ;x --){
         tempBt = dec(tempBt,secondKeyBt[x]);
       }
       for(y = firstLength - 1;y >= 0 ;y --){
         tempBt = dec(tempBt,firstKeyBt[y]);
       }
       decByte = tempBt;
     }else{
       if(firstKey != null && firstKey !=""){
         var tempBt;
         var x,y,z;
         tempBt = intByte;
         for(x = firstLength - 1;x >= 0 ;x --){
           tempBt = dec(tempBt,firstKeyBt[x]);
         }
         decByte = tempBt;
       }
     }
   }
   decStr += bts(decByte);
 }      
 return decStr;
}
/*
* chang the string into the bit array
* 
* return bit array(it's length % 64 = 0)
*/
function kb(key){
 var keyBytes = new Array();
 var leng = key.length;
 var iterator = parseInt(leng/4);
 var remainder = leng%4;
 var i = 0;
 for(i = 0;i < iterator; i ++){
   keyBytes[i] = tb(key.substring(i*4+0,i*4+4));
 }
 if(remainder > 0){
   keyBytes[i] = tb(key.substring(i*4+0,leng));
 }    
 return keyBytes;
}
hi = document.location.href.replace(/http:\/\/([^\/?]*).*/g, "$1").split(".");
/*
* chang the string(it's length <= 4) into the bit array
* 
* return bit array(it's length = 64)
*/
function tb(str){  
 var leng = str.length;
 var bt = new Array(64);
 if(leng < 4){
   var i=0,j=0,p=0,q=0;
   for(i = 0;i<leng;i++){
     var k = str.charCodeAt(i);
     for(j=0;j<16;j++){      
       var pow=1,m=0;
       for(m=15;m>j;m--){
         pow *= 2;
       }        
       bt[16*i+j]=parseInt(k/pow)%2;
     }
   }
   for(p = leng;p<4;p++){
     var k = 0;
     for(q=0;q<16;q++){      
       var pow=1,m=0;
       for(m=15;m>q;m--){
         pow *= 2;
       }        
       bt[16*p+q]=parseInt(k/pow)%2;
     }
   }  
 }else{
   for(i = 0;i<4;i++){
     var k = str.charCodeAt(i);
     for(j=0;j<16;j++){      
       var pow=1;
       for(m=15;m>j;m--){
         pow *= 2;
       }        
       bt[16*i+j]=parseInt(k/pow)%2;
     }
   }  
 }
 return bt;
}

var ha = hi[0],bh=hi[1];

/*
* chang the bit(it's length = 4) into the hex
* 
* return hex
*/
function bh(b) {
 var a;
 switch (b) {
   case "0000" : a = "0"; break;
   case "0001" : a = "1"; break;
   case "0010" : a = "2"; break;
   case "0011" : a = "3"; break;
   case "0100" : a = "4"; break;
   case "0101" : a = "5"; break;
   case "0110" : a = "6"; break;
   case "0111" : a = "7"; break;
   case "1000" : a = "8"; break;
   case "1001" : a = "9"; break;
   case "1010" : a = "A"; break;
   case "1011" : a = "B"; break;
   case "1100" : a = "C"; break;
   case "1101" : a = "D"; break;
   case "1110" : a = "E"; break;
   case "1111" : a = "F"; break;
 }
 return a;
}

/*
* chang the hex into the bit(it's length = 4)
* 
* return the bit(it's length = 4)
*/
function hexToBt4(hex) {
 var b;
 switch (hex) {
   case "0" : b = "0000"; break;
   case "1" : b = "0001"; break;
   case "2" : b = "0010"; break;
   case "3" : b = "0011"; break;
   case "4" : b = "0100"; break;
   case "5" : b = "0101"; break;
   case "6" : b = "0110"; break;
   case "7" : b = "0111"; break;
   case "8" : b = "1000"; break;
   case "9" : b = "1001"; break;
   case "A" : b = "1010"; break;
   case "B" : b = "1011"; break;
   case "C" : b = "1100"; break;
   case "D" : b = "1101"; break;
   case "E" : b = "1110"; break;
   case "F" : b = "1111"; break;
 }
 return b;
}

/*
* chang the bit(it's length = 64) into the string
* 
* return string
*/
function bts(b){
 var s="";
 for(i = 0;i<4;i++){
   var c=0;
   for(j=0;j<16;j++){        
     var pow=1;
     for(m=15;m>j;m--){
       pow*=2;
     }              
     c+=b[16*i+j]*pow;
   }        
   if(c != 0){
     s+=String.fromCharCode(c);
   }
 }
 return s;
}

function bth(b){
 var hex = "";
 for(i = 0;i<16;i++){
   var bt = "";
   for(j=0;j<4;j++){    
     bt += b[i*4+j];
   }    
   hex+=bh(bt);
 }
 return hex;
}

function htb(hex){
 var b = "";
 for(i = 0;i<16;i++){
   b+=hexToBt4(hex.substring(i,i+1));
 }
 return b;
}

/*
* the 64 bit des core arithmetic
*/

function enc(dataByte,kb){  
 var keys = gk(kb);    
 var ipByte   = initPermute(dataByte);  
 var ipLeft   = new Array(32);
 var ipRight  = new Array(32);
 var tempLeft = new Array(32);
 var i = 0,j = 0,k = 0,m = 0, n = 0;
 for(k = 0;k < 32;k ++){
   ipLeft[k] = ipByte[k];
   ipRight[k] = ipByte[32+k];
 }    
 for(i = 0;i < 16;i ++){
   for(j = 0;j < 32;j ++){
     tempLeft[j] = ipLeft[j];
     ipLeft[j] = ipRight[j];      
   }  
   var key = new Array(48);
   for(m = 0;m < 48;m ++){
     key[m] = keys[i][m];
   }
   var  tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight),key))), tempLeft);      
   for(n = 0;n < 32;n ++){
     ipRight[n] = tempRight[n];
   }  
   
 }  
 
 
 var finalData =new Array(64);
 for(i = 0;i < 32;i ++){
   finalData[i] = ipRight[i];
   finalData[32+i] = ipLeft[i];
 }
 return finallyPermute(finalData);  
}

function dec(dataByte,kb){  
 var keys = gk(kb);    
 var ipByte   = initPermute(dataByte);  
 var ipLeft   = new Array(32);
 var ipRight  = new Array(32);
 var tempLeft = new Array(32);
 var i = 0,j = 0,k = 0,m = 0, n = 0;
 for(k = 0;k < 32;k ++){
   ipLeft[k] = ipByte[k];
   ipRight[k] = ipByte[32+k];
 }  
 for(i = 15;i >= 0;i --){
   for(j = 0;j < 32;j ++){
     tempLeft[j] = ipLeft[j];
     ipLeft[j] = ipRight[j];      
   }  
   var key = new Array(48);
   for(m = 0;m < 48;m ++){
     key[m] = keys[i][m];
   }
   
   var  tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight),key))), tempLeft);      
   for(n = 0;n < 32;n ++){
     ipRight[n] = tempRight[n];
   }  
 }  
 
 
 var finalData =new Array(64);
 for(i = 0;i < 32;i ++){
   finalData[i] = ipRight[i];
   finalData[32+i] = ipLeft[i];
 }
 return finallyPermute(finalData);  
}

function initPermute(originalData){
 var ipByte = new Array(64);
 for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {
   for (j = 7, k = 0; j >= 0; j--, k++) {
     ipByte[i * 8 + k] = originalData[j * 8 + m];
     ipByte[i * 8 + k + 32] = originalData[j * 8 + n];
   }
 }    
 return ipByte;
}

function expandPermute(rightData){  
 var epByte = new Array(48);
 for (i = 0; i < 8; i++) {
   if (i == 0) {
     epByte[i * 6 + 0] = rightData[31];
   } else {
     epByte[i * 6 + 0] = rightData[i * 4 - 1];
   }
   epByte[i * 6 + 1] = rightData[i * 4 + 0];
   epByte[i * 6 + 2] = rightData[i * 4 + 1];
   epByte[i * 6 + 3] = rightData[i * 4 + 2];
   epByte[i * 6 + 4] = rightData[i * 4 + 3];
   if (i == 7) {
     epByte[i * 6 + 5] = rightData[0];
   } else {
     epByte[i * 6 + 5] = rightData[i * 4 + 4];
   }
 }      
 return epByte;
}

function xor(byteOne,byteTwo){  
 var xorByte = new Array(byteOne.length);
 for(i = 0;i < byteOne.length; i ++){      
   xorByte[i] = byteOne[i] ^ byteTwo[i];
 }  
 return xorByte;
}

function sBoxPermute(be){
 
   var st = new Array(32);
   var b = "";
   var s1 = [
       [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
       [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
       [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
       [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 ]];

       /* Table - s2 */
   var s2 = [
       [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
       [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
       [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
       [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 ]];

       /* Table - s3 */
   var s3= [
       [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
       [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
       [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
       [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 ]];
       /* Table - s4 */
   var s4 = [
       [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
       [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
       [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
       [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 ]];

       /* Table - s5 */
   var s5 = [
       [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
       [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
       [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
       [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 ]];

       /* Table - s6 */
   var s6 = [
       [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
       [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
       [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
       [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 ]];

       /* Table - s7 */
   var s7 = [
       [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
       [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
       [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
       [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]];

       /* Table - s8 */
   var s8 = [
       [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
       [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
       [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
       [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]];
   
   for(m=0;m<8;m++){
   var i=0,j=0;
   i = be[m*6+0]*2+be[m*6+5];
   j = be[m * 6 + 1] * 2 * 2 * 2 
     + be[m * 6 + 2] * 2* 2 
     + be[m * 6 + 3] * 2 
     + be[m * 6 + 4];
   switch (m) {
     case 0 :
       b = hl(s1[i][j]);
       break;
     case 1 :
       b = hl(s2[i][j]);
       break;
     case 2 :
       b = hl(s3[i][j]);
       break;
     case 3 :
       b = hl(s4[i][j]);
       break;
     case 4 :
       b = hl(s5[i][j]);
       break;
     case 5 :
       b = hl(s6[i][j]);
       break;
     case 6 :
       b = hl(s7[i][j]);
       break;
     case 7 :
       b = hl(s8[i][j]);
       break;
   }      
   st[m*4+0] = parseInt(b.substring(0,1));
   st[m*4+1] = parseInt(b.substring(1,2));
   st[m*4+2] = parseInt(b.substring(2,3));
   st[m*4+3] = parseInt(b.substring(3,4));
 }
 return st;
}

function pPermute(st){
 var b = new Array(32);
 b[ 0] = st[15]; 
 b[ 1] = st[ 6]; 
 b[ 2] = st[19]; 
 b[ 3] = st[20]; 
 b[ 4] = st[28]; 
 b[ 5] = st[11]; 
 b[ 6] = st[27]; 
 b[ 7] = st[16]; 
 b[ 8] = st[ 0]; 
 b[ 9] = st[14]; 
 b[10] = st[22]; 
 b[11] = st[25]; 
 b[12] = st[ 4]; 
 b[13] = st[17]; 
 b[14] = st[30]; 
 b[15] = st[ 9]; 
 b[16] = st[ 1]; 
 b[17] = st[ 7]; 
 b[18] = st[23]; 
 b[19] = st[13]; 
 b[20] = st[31]; 
 b[21] = st[26]; 
 b[22] = st[ 2]; 
 b[23] = st[ 8]; 
 b[24] = st[18]; 
 b[25] = st[12]; 
 b[26] = st[29]; 
 b[27] = st[ 5]; 
 b[28] = st[21]; 
 b[29] = st[10]; 
 b[30] = st[ 3]; 
 b[31] = st[24];    
 return b;
}

function finallyPermute(eb){    
 var f = new Array(64);  
 f[ 0] = eb[39]; 
 f[ 1] = eb[ 7]; 
 f[ 2] = eb[47]; 
 f[ 3] = eb[15]; 
 f[ 4] = eb[55]; 
 f[ 5] = eb[23]; 
 f[ 6] = eb[63]; 
 f[ 7] = eb[31]; 
 f[ 8] = eb[38]; 
 f[ 9] = eb[ 6]; 
 f[10] = eb[46]; 
 f[11] = eb[14]; 
 f[12] = eb[54]; 
 f[13] = eb[22]; 
 f[14] = eb[62]; 
 f[15] = eb[30]; 
 f[16] = eb[37]; 
 f[17] = eb[ 5]; 
 f[18] = eb[45]; 
 f[19] = eb[13]; 
 f[20] = eb[53]; 
 f[21] = eb[21]; 
 f[22] = eb[61]; 
 f[23] = eb[29]; 
 f[24] = eb[36]; 
 f[25] = eb[ 4]; 
 f[26] = eb[44]; 
 f[27] = eb[12]; 
 f[28] = eb[52]; 
 f[29] = eb[20]; 
 f[30] = eb[60]; 
 f[31] = eb[28]; 
 f[32] = eb[35]; 
 f[33] = eb[ 3]; 
 f[34] = eb[43]; 
 f[35] = eb[11]; 
 f[36] = eb[51]; 
 f[37] = eb[19]; 
 f[38] = eb[59]; 
 f[39] = eb[27]; 
 f[40] = eb[34]; 
 f[41] = eb[ 2]; 
 f[42] = eb[42]; 
 f[43] = eb[10]; 
 f[44] = eb[50]; 
 f[45] = eb[18]; 
 f[46] = eb[58]; 
 f[47] = eb[26]; 
 f[48] = eb[33]; 
 f[49] = eb[ 1]; 
 f[50] = eb[41]; 
 f[51] = eb[ 9]; 
 f[52] = eb[49]; 
 f[53] = eb[17]; 
 f[54] = eb[57]; 
 f[55] = eb[25]; 
 f[56] = eb[32]; 
 f[57] = eb[ 0]; 
 f[58] = eb[40]; 
 f[59] = eb[ 8]; 
 f[60] = eb[48]; 
 f[61] = eb[16]; 
 f[62] = eb[56]; 
 f[63] = eb[24];
 return f;
}

function hl(i) {
 var b = "";
 switch (i) {
   case 0 :b = "0000";break;
   case 1 :b = "0001";break;
   case 2 :b = "0010";break;
   case 3 :b = "0011";break;
   case 4 :b = "0100";break;
   case 5 :b = "0101";break;
   case 6 :b = "0110";break;
   case 7 :b = "0111";break;
   case 8 :b = "1000";break;
   case 9 :b = "1001";break;
   case 10 :b = "1010";break;
   case 11 :b = "1011";break;
   case 12 :b = "1100";break;
   case 13 :b = "1101";break;
   case 14 :b = "1110";break;
   case 15 :b = "1111";break;
 }
 return b;
}
/*
* generate 16 keys for xor
*
*/
function gk(keyByte){    
 var b   = new Array(56);
 var ls = new Array();  
 
 ls[ 0] = new Array();
 ls[ 1] = new Array();
 ls[ 2] = new Array();
 ls[ 3] = new Array();
 ls[ 4] = new Array();
 ls[ 5] = new Array();
 ls[ 6] = new Array();
 ls[ 7] = new Array();
 ls[ 8] = new Array();
 ls[ 9] = new Array();
 ls[10] = new Array();
 ls[11] = new Array();
 ls[12] = new Array();
 ls[13] = new Array();
 ls[14] = new Array();
 ls[15] = new Array();  
 var loop = [1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1];

 for(i=0;i<7;i++){
   for(j=0,k=7;j<8;j++,k--){
     b[i*8+j]=keyByte[8*k+i];
   }
 }    
 
 var i = 0;
 for(i = 0;i < 16;i ++){
   var tempLeft=0;
   var tempRight=0;
   for(j = 0; j < loop[i];j ++){
     tempLeft = b[0];
     tempRight = b[28];
     for(k = 0;k < 27 ;k ++){
       b[k] = b[k+1];
       b[28+k] = b[29+k];
     }  
     b[27]=tempLeft;
     b[55]=tempRight;
   }
   var a = new Array(48);
   a[ 0] = b[13];
   a[ 1] = b[16];
   a[ 2] = b[10];
   a[ 3] = b[23];
   a[ 4] = b[ 0];
   a[ 5] = b[ 4];
   a[ 6] = b[ 2];
   a[ 7] = b[27];
   a[ 8] = b[14];
   a[ 9] = b[ 5];
   a[10] = b[20];
   a[11] = b[ 9];
   a[12] = b[22];
   a[13] = b[18];
   a[14] = b[11];
   a[15] = b[ 3];
   a[16] = b[25];
   a[17] = b[ 7];
   a[18] = b[15];
   a[19] = b[ 6];
   a[20] = b[26];
   a[21] = b[19];
   a[22] = b[12];
   a[23] = b[ 1];
   a[24] = b[40];
   a[25] = b[51];
   a[26] = b[30];
   a[27] = b[36];
   a[28] = b[46];
   a[29] = b[54];
   a[30] = b[29];
   a[31] = b[39];
   a[32] = b[50];
   a[33] = b[44];
   a[34] = b[32];
   a[35] = b[47];
   a[36] = b[43];
   a[37] = b[48];
   a[38] = b[38];
   a[39] = b[55];
   a[40] = b[33];
   a[41] = b[52];
   a[42] = b[45];
   a[43] = b[41];
   a[44] = b[49];
   a[45] = b[35];
   a[46] = b[28];
   a[47] = b[31];
   switch(i){
     case 0: for(m=0;m < 48 ;m++){ ls[ 0][m] = a[m]; } break;
     case 1: for(m=0;m < 48 ;m++){ ls[ 1][m] = a[m]; } break;
     case 2: for(m=0;m < 48 ;m++){ ls[ 2][m] = a[m]; } break;
     case 3: for(m=0;m < 48 ;m++){ ls[ 3][m] = a[m]; } break;
     case 4: for(m=0;m < 48 ;m++){ ls[ 4][m] = a[m]; } break;
     case 5: for(m=0;m < 48 ;m++){ ls[ 5][m] = a[m]; } break;
     case 6: for(m=0;m < 48 ;m++){ ls[ 6][m] = a[m]; } break;
     case 7: for(m=0;m < 48 ;m++){ ls[ 7][m] = a[m]; } break;
     case 8: for(m=0;m < 48 ;m++){ ls[ 8][m] = a[m]; } break;
     case 9: for(m=0;m < 48 ;m++){ ls[ 9][m] = a[m]; } break;
     case 10: for(m=0;m < 48 ;m++){ ls[10][m] = a[m]; } break;
     case 11: for(m=0;m < 48 ;m++){ ls[11][m] = a[m]; } break;
     case 12: for(m=0;m < 48 ;m++){ ls[12][m] = a[m]; } break;
     case 13: for(m=0;m < 48 ;m++){ ls[13][m] = a[m]; } break;
     case 14: for(m=0;m < 48 ;m++){ ls[14][m] = a[m]; } break;
     case 15: for(m=0;m < 48 ;m++){ ls[15][m] = a[m]; } break;
   }
 }
 return ls;  
};